Overload subscript operator [] in C ++
The subscript operator [] in C ++ is often used to access array elements. This operator can be overloaded to enhance existing functionality in arrays in C ++.
The subscript operator [] in C ++ is often used to access array elements. This operator can be overloaded to enhance an existing feature of arrays in C ++ (so it can be called an array index operator).
The following example illustrates how to load the subscript operator [] in C ++:
#include using namespace std ; const int KICHCO = 15 ; class ViDuMang { private : int mang [ KICHCO ]; public : ViDuMang () { register int i ; for ( i = 0 ; i < KICHCO ; i ++) { mang [ i ] = i ; } } int & operator []( int i ) { if ( i > KICHCO ) { cout << "n======================n" << endl ; cout << "Chi muc vuot gioi han!!" << endl ; // Tra ve phan tu dau tien. return mang [ 0 ]; } return mang [ i ]; } }; int main () { ViDuMang V ; cout << "Gia tri cua V[3] la: " << V [ 3 ] << endl ; cout << "Gia tri cua V[6] la: " << V [ 6 ]<< endl ; cout << "Gia tri cua V[16] la: " << V [ 16 ]<< endl ; return 0 ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Load the operator stack call the function () in C ++
Next lesson: Overload member access operator (->) in C ++
4 ★ | 1 Vote
You should read it
- Load the operator stack to call the function () in C ++
- Overload class member access operator (->) in C ++
- Overload binary operators in C ++
- Load the stack of assignment operators in C ++
- Load operator ++ and - in C ++
- Operator in PHP
- Operator in C ++
- Operator overloading in C #
- ! = and! == What is the difference in JavaScript?
- Operator overload and Load overlap in C ++
- Stack operator in Python
- Operator in JavaScript
Maybe you are interested
6 ways to fix the error 'PowerPoint found a problem with the content'
Sample painting of Hanoi in my heart, simple landscape
PowerPoint 2016: Working with lists in PowerPoint
How to Paste an Image into a Selection in Photoshop
Europe develops satellite internet system to replace Starlink
Intel releases new driver update package with 6GHz optimization and many other improvements